/*
 * 
 *
 *
 */
package com.hboxs.asl.dao.impl;

import com.hboxs.asl.Page;
import com.hboxs.asl.Pageable;
import com.hboxs.asl.dao.ReturnsRecordDao;
import com.hboxs.asl.entity.ReturnsRecord;
import org.springframework.stereotype.Repository;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

/**
 * Dao - 退货记录
 */
@Repository("returnsRecordDaoImpl")
public class ReturnsRecordDaoImpl extends BaseDaoImpl<ReturnsRecord, Long> implements ReturnsRecordDao {

    @Override
    public Page<ReturnsRecord> findPage(ReturnsRecord.Reason reason, ReturnsRecord.Status status, Pageable pageable) {
        CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
        CriteriaQuery<ReturnsRecord> criteriaQuery = criteriaBuilder.createQuery(ReturnsRecord.class);
        Root<ReturnsRecord> root = criteriaQuery.from(ReturnsRecord.class);
        criteriaQuery.select(root);
        Predicate restrictions = criteriaBuilder.conjunction();
        if (reason != null) {
            restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.equal(root.get("reason"), reason));
        }
        if (status != null) {
            restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.equal(root.get("status"), status));
        }
        criteriaQuery.where(restrictions);
        return super.findPage(criteriaQuery, pageable);
    }


}
